import { Component, OnInit, Input } from '@angular/core';
import { Hero } from 'src/app/domain/heroes/hero';
import { ActivatedRoute } from '@angular/router';
import { HeroService } from 'src/app/service/hero.service';
import { Location } from '@angular/common';

@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.scss']
})
export class HeroDetailComponent implements OnInit {
  @Input() hero: Hero
  constructor(
    private route: ActivatedRoute,
    private heroService: HeroService,
    private location: Location
  ) { }

  ngOnInit() {
    this.getHero()
  }

  save() {
    this.heroService.updateHero(this.hero).subscribe(()=>this.location.back())
  }
  
  delete() {
    this.heroService.deleteHero(this.hero).subscribe(()=>this.location.back())
  }
  getHero() {
    const id = +this.route.snapshot.paramMap.get('id')
    this.heroService.getHeroe(id).subscribe(hero=>this.hero=hero)
  }

  goBack() {
    this.location.back()
  }

  
}
